<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
      <link rel="stylesheet" href="https://gw.alipayobjects.com/os/rmsportal/PqLCOJpqoOUfuPRacUzE.css" />
    <title>自定义图例组件</title>
    <style> ::-webkit-scrollbar{display:none;}html,body{overflow:hidden;margin:0;}
    	#map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>
<div id="map"></div>
<script>/*Fixing iframe window.innerHeight 0 issue in Safari*/ document.body.clientHeight; </script>
<script src="https://webapi.amap.com/maps?v=1.4.8&key=15cd8a57710d40c9b7c0e3cc120f1200&plugin=Map3D"></script>
<script src="https://gw.alipayobjects.com/os/antv/assets/lib/jquery-3.2.1.min.js"></script>
<script src="../build/L7.js"></script>
<style>
.info {
	padding: 6px 8px;
	font: 14px/16px Arial, Helvetica, sans-serif;
	background: white;
	background: rgba(255,255,255,0.8);
	box-shadow: 0 0 15px rgba(0,0,0,0.2);
	border-radius: 5px;
}
.info h4 {
	margin: 0 0 5px;
	color: #777;
}
.legend {
	line-height: 18px;
	color: #555;
}
.legend i {
	width: 18px;
	height: 18px;
	float: left;
	margin-right: 8px;
	opacity: 0.7;
}
</style>
<script>
  var scene = new L7.Scene({
    id: 'map',
    mapStyle: 'light', // 样式URL
    center: [120.19382669582967, 30.258134],
    pitch: 0,
    zoom: 4,
    zoomControl: false,
    scaleControl: false
  });
  scene.on('loaded', function() {
    var colors = ["#FFF5B8", "#FFDC7D", "#FFAB5C", "#F27049", "#D42F31", "#730D1C"];
    $.getJSON('https://gw.alipayobjects.com/os/rmsportal/JToMOWvicvJOISZFCkEI.json', function(city) {
      var layer = scene.PolygonLayer().source(city).color('pm2_5_24h', function(p) {
        if (p > 120) {
          return colors[5];
        } else if (p > 65) {
          return colors[4];
        } else if (p > 30) {
          return colors[3];
        } else if (p > 15) {
          return colors[2];
        } else if (p > 8) {
          return colors[1];
        } else {
          return colors[0];
        }
      }).shape('fill').active(true).style({
        opacity: 1
      }).render();
      var legendinfo = layer.getLegendCfg('pm2_5_24h');
  
      var legend = new L7.Control.Base({
        position: 'bottomright'
      });
      legend.onAdd = function(scene) {
        const el = document.createElement('div');
        el.className = 'info legend';
        const grades = [0, 8, 15, 30, 65, 120 ];
        for (var i = 0; i < grades.length; i++) {
		       el.innerHTML +=
            '<i style="background:' + colors[i] + '"></i> ' +
            grades[i] + (grades[i + 1] ? '&ndash;' + grades[i + 1] + '<br>' : '+');
	          }
        return el;
       };
       legend.addTo(scene);
    });
  });
</script>
</body>
</html>
